﻿using System;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows;
using System.Windows.Input;

namespace UI.Controls.Popup
{
    public class PopUp
    {
        readonly Visual _visual;
        BackgroundShade _shade;
        readonly PopUpControl _adorner;
        readonly AdornerLayer _layer;

        MouseButtonEventHandler closeEventHandler;

        bool _closedOnLeftClick;
        public bool IsClosedOnLeftClick 
        { 
            get
            {
                return _closedOnLeftClick;
            }
            set
            {
                if (_closedOnLeftClick != value)
                {
                    _closedOnLeftClick = value;
                    SetCloseBehaviour();
                }
            }
        }
        
        public Grid Content
        {
            get
            {
                return _shade.LayoutRoot;
            }
        }
        public Brush ShadeColor
        {
            get
            {
                return _shade.Background;
            }
            set
            {
                _shade.Background = value;
            }
        }
        public double ShadeOpacity
        {
            get
            {
                return _shade.Opacity;
            }
            set
            {
                _shade.Opacity = value;
            }
        }

        public PopUp(Visual visual)
        {
            _visual = visual;
            _adorner = new PopUpControl(_visual as UIElement);
            _layer = AdornerLayer.GetAdornerLayer(visual);

            closeEventHandler = delegate
                                        {
                                            Close();
                                        };

            // Close adorner on left click
            SetCloseBehaviour();

            ((FrameworkElement)_adorner.AdornedElement).SizeChanged += AdornedElement_SizeChanged;

            // Create shaded background
            _shade = new BackgroundShade();
            AdornedElement_SizeChanged(null, null);
        }

        void SetCloseBehaviour()
        {
            if (IsClosedOnLeftClick)
            {
                _adorner.MouseLeftButtonDown += closeEventHandler;
            }
            else
            {
                _adorner.MouseLeftButtonDown -= closeEventHandler;
            }
        }

        public void Close()
        {
            if (_adorner.IsVisible)
            {
                _layer.Remove(_adorner);
            }
        }

        public void Show()
        {
            if (_shade.LayoutRoot.Children.Count == 0)
            {
                // Create text
                TextBlock text = new TextBlock();
                text.Foreground = Brushes.White;
                text.Text = "Text is here";
                // Register text
                _shade.LayoutRoot.Children.Add(text);
            }

            // Display adorner
            _adorner.Content = _shade;
            if (!_adorner.IsVisible)
            {
                _layer.Add(_adorner);
            }

        }

        /// <summary>
        /// Adorned element's size changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void AdornedElement_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            _shade.Width = _adorner.AdornedElement.RenderSize.Width;
            _shade.Height = _adorner.AdornedElement.RenderSize.Height;
        }

    }
}
